package com.android.boncen.commonFrame;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioGroup;

import com.android.boncen.commonFrame.fragment.DoctorFragment;
import com.android.boncen.commonFrame.fragment.RecordFragment;
import com.android.boncen.commonFrame.fragment.knowledgeFragment;
import com.android.boncen.commonFrame.fragment.recommendFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 这个案例中遇到的问题
 * 一个是V4包中的Fragment和android.app包中的Fragment 存在兼容性问题，比如V4包的参数不能传递给android.app的Fragment
 * 另一个是不一定要继承baseFragment，自己写的Fragment类中重写oncreateView ,返回一个view即可
 * 以及 fragmentTansaction的add参数要对
 * Created by boncen on 2016/12/9.
 */
public class MainActivity extends FragmentActivity {

    @BindView(R.id.rg_main)
    RadioGroup radioGroup;

    private int position; //表示下面按钮的位置
    private ArrayList<Fragment> arrayList = new ArrayList<>();
    private Fragment lastFrament;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        arrayList.add(new RecordFragment());
        arrayList.add(new recommendFragment());
        arrayList.add(new knowledgeFragment());
        arrayList.add(new DoctorFragment());

        radioGroup.setOnCheckedChangeListener(new myRadioGroupListener());
        radioGroup.check(R.id.rb_1);
    }

    private class myRadioGroupListener implements RadioGroup.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            switch (i) {
                case R.id.rb_1:
                    position = 0;
                    break;
                case R.id.rb_2:
                    position = 1;
//                    title.setText("饮食与运动推荐");
                    break;
                case R.id.rb_3:
                    position = 2;
//                    title.setText("养生知识");
                    break;
                case R.id.rb_4:
                    position = 3;
//                    title.setText("医生");
                    break;
                default:
                    position = 0;
                    break;
            }
            Fragment to = getFragment();
            switchFragment(lastFrament, to);
        }
    }

    private Fragment getFragment() {
        Fragment fragment = arrayList.get(position);
        return fragment;
    }

    private void switchFragment(Fragment from, Fragment to) {
        if (from != to) {
            lastFrament = to;
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) {   //to 还没有被添加
                //隐藏from
                if (from != null) {
                    ft.hide(from);
                }
                if (to != null) {
                    ft.add(R.id.fl_content, to).commit();
                }

            } else {  //to已经被添加
                if (from != null) {
                    ft.hide(from);
                }
                if (to != null) {
                    ft.show(to).commit();
                }
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            int index = intent.getIntExtra("index", -1);
            if (index == 0) {
                position = 0;
                Fragment to = getFragment();
                switchFragment(lastFrament, to);
                radioGroup.check(R.id.rb_1);
            }
        }
    }
}
